<template>
	<view class="view">
		<view class="address" @click="selectAddress()">
			<image class="icon" src="/static/images/position.png"></image>
			<view class="info">
				<view>{{address.name}}<text>{{address.phone}}</text></view>
				<view>
					{{address.provinceName || ''}}{{address.cityName || ''}}{{address.areaName || ''}}{{address.address || ''}}
				</view>
			</view>
			<image class="go" src="/static/images/ic_next.png"></image>
		</view>
		<image style="width: 100%;" src="/static/images/shment.png" mode="widthFix"></image>
		<view class="product">
			<image class="pic" :src="data.productPic"></image>
			<view class="content">
				<view class="name">{{data.productName}}</view>
				<view class="price">{{data.price || 0}}积分</view>
				<view class="box">
					<view class="operate" :class="count > 1 ? '' : 'gray'" @click="count > 1 ? editCount(-1) : null">-</view>
					<view class="value">{{count}}</view>
					<view class="operate" :class="total + data.price < account.accountBalance ? '' : 'gray'" @click="total + data.price < account.accountBalance ? editCount(1) : null">+</view>
				</view>
			</view>
		</view>
		<view class="bottom">
			<view class="total">总积分：<text>{{total}}积分</text></view>
			<view class="btn" @click="sureRecharge()">确认兑换</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: null,
				count: 1,
				total: 0,
				data: {},
				address: {},
				account: {},
				canAdd: false
			}
		},
		onLoad(options) {
			this.id = options.id;
			this.init();
		},
		methods: {
			init() {
				this.$api.http('/user/userAccountInfo/getAccountInfoByUserId', 'GET', {
					userId: uni.getStorageSync('userInfo').id
				}, res => {
					if (res.data.code == 200) {
						this.account = res.data.result;
					} else {
						this.$Toast(res.data.message || '请求失败，请重试！');
					}
				});
				this.$api.http('/user/userAddress/defaultAddress', 'GET', {}, res => {
					if (res.data.code == 200) {
						this.address = res.data.result;
					} else {
						this.$Toast(res.data.message || '请求失败，请重试！');
					}
				});
				this.$api.http('/product/pmsIntegralProduct/api/n/details', 'GET', {
					productId: this.id
				}, res => {
					if (res.data.code == 200) {
						this.data = res.data.result;
						this.total = this.data.price;
					} else {
						this.$Toast(res.data.message || '请求失败，请重试！');
					}
				});
			},
			selectAddress() {
				const that = this;
				uni.navigateTo({
					url: '/pages/other/address/address?type=1',
					events: {
						acceptDataEvent: function(data) {
							that.address = data;
						}
					}
				});
			},
			editCount(num) {
				this.count += num;
				this.total = this.count * this.data.price;
			},
			sureRecharge() {
				if (!this.address.id) {
					return this.$Toast('请选择收货地址');
				}
				this.$api.http('/product/pmsIntegralProduct/redeemNow', 'GET', {
					addressId: this.address.id,
					productId: this.id,
					productNum: this.count,
					userId: uni.getStorageSync('userInfo').id
				}, res => {
					if (res.data.code == 200) {
						this.$Toast('兑换成功');
						uni.navigateBack();
					} else {
						this.$Toast(res.data.message || '兑换失败，请重试！');
					}
				});
			},
		},
	}
</script>

<style lang="less" scoped>
	.view {
		width: 100%;
		background-color: #F1F1F1;

		.address {
			display: flex;
			flex-direction: row;
			align-items: center;
			padding: 30rpx;
			background-color: #FFFFFF;

			.icon {
				width: 80rpx;
				height: 80rpx;
				margin-right: 30rpx;
			}

			.info {
				flex: 1;
				font-size: 26rpx;
				color: #666666;

				view {
					&:first-child {
						font-size: 28rpx;
						color: #333333;

						text {
							margin-left: 30rpx;
						}
					}
				}
			}

			.go {
				width: 20rpx;
				height: 35rpx;
			}
		}

		.product {
			display: flex;
			flex-direction: row;
			align-items: center;
			padding: 20rpx 30rpx;
			background-color: #FFFFFF;

			.pic {
				width: 180rpx;
				height: 180rpx;
				margin-right: 20rpx;
			}

			.content {
				flex: 1;
				
				.name {
					font-size: 30rpx;
					color: #333333;
				}
				
				.price {
					margin: 15rpx 0;
					font-size: 26rpx;
					color: #E5AE54;
				}
				
				.box {
					display: flex;
					flex-direction: row;
					align-items: center;
					width: 150rpx;
					line-height: 50rpx;
					color: #333333;
					border: 1rpx solid #F1F1F1;
					
					.operate {
						flex: 1;
						text-align: center;
						
						&.gray {
							color: #CDCDCD;
						}
					}
					
					.value {
						flex: 1;
						text-align: center;
					}
				}
			}
		}

		.bottom {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			height: 120rpx;
			padding: 0 20rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			background-color: #FFFFFF;
			border-top: 1rpx solid #F1F1F1;

			.total {
				flex: 1;

				text {
					color: red;
				}
			}

			.btn {
				width: 200rpx;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				font-size: 28rpx;
				color: #FFFFFF;
				background-color: #32C59A;
				border-radius: 40rpx;
			}
		}
	}
</style>
